package com.mark.spring.annotation.value;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.util.Assert;

/**
 * @Description: @value 注解的引导类
 * @Author: Mark
 * @CreateDate: 2019/9/22 17:52
 * @Version: 3.00.001
 * @Copyright : 豆浆油条个人非正式工作室
 *
 * 读取配置文件步骤
 * 1.定义编写 properties文件
 * 2.加载配置文件@PropertySource
 * 3.使用@Value注解获取值
 */
@ComponentScan("com.mark.spring.annotation.value")
public class ValueBootstrap {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ValueBootstrap.class);
        Cookie cookie = applicationContext.getBean(Cookie.class);
        Assert.notNull(cookie,"cookie bean为空");
        System.out.println(cookie.toString());

        //从env中获取配置信息
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        String name = environment.getProperty("cookie.name");
        String price = environment.getProperty("cookie.price");
        environment.resolvePlaceholders("${cookie.name}");



        System.out.println("name:" + name);
        System.out.println("price:" + price);

        applicationContext.close();
    }

}
